<?php

namespace App\Service;
/**
 * 单例模式
 *
 * @author WangBin
 */
class Singleton
{
    private static $instance = [];

    /**
     * 获取自身单例对象
     *
     * @return static
     */
    public static function getInstance() {
        $classCame = get_called_class();
        if (isset(self::$instance[$classCame])) {
            return self::$instance[$classCame];
        }
        self::$instance[$classCame] = new $classCame;
        return self::$instance[$classCame];
    }

    /**
     * 统一列表返回格式
     *
     * @param $collection
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    protected function toList($collection, $page = 1, $pageSize = 20) {
        return [
            'total' => $collection->count(),
            'list' => $collection->offset(($page -1) * $pageSize)->limit($pageSize)->get()
        ];
    }

    protected function getPage($param)
    {
        return $param['page'] ?? 1;
    }

    protected function getPageSize($param)
    {
        return $param['page_size'] ?? 20;
    }
}